﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.JuicyLib.MarketData.Structure;

namespace Algobox.JuicyLib.Apps.EODData.Files
{
    public class Splits : Dictionary<string, Split>
    {
        public Splits(ref string file)
        {
            LoadFile(ref file);
        }

        public const string FILENAME_LSE = "ftp://ftp.eoddata.com/Splits/LSE.txt";
        public const string FILENAME_MLSE = "ftp://ftp.eoddata.com/Splits/MLSE.txt";
        public const string FILENAME_FOREX = "ftp://ftp.eoddata.com/Splits/FOREX.txt";
        public const string FILENAME_NYSE = "ftp://ftp.eoddata.com/Splits/NYSE.txt";
        public const string FILENAME_NASDAQ = "ftp://ftp.eoddata.com/Splits/NASDAQ.txt";
        public const string FILENAME_AMEX = "ftp://ftp.eoddata.com/Splits/AMEX.txt";
        public const string FILENAME_OPRA = "ftp://ftp.eoddata.com/Splits/OPRA.txt";
        public const string FILENAME_TSX = "ftp://ftp.eoddata.com/Splits/TSX.txt";
        public const string FILENAME_TSXV = "ftp://ftp.eoddata.com/Splits/TSXV.txt";

        const char EOD_SEPERATOR = '\t';
        const char EOD_RATIOSPLITER = '-';
        const int EOD_COLUMNS = 3;

        const int EOD_SYMBOL = 0;
        const int EOD_DATE = 1;
        const int EOD_RATIO = 2;

        void LoadFile(ref string file)
        {
            try
            {
                string[] elements;
                string[] ratio;
                Split split;
                string symbol;
                int recieves, from;
                DateTime date;

                string[] lines = file.Split('\n');
                foreach (string line in lines)
                {
                    elements = line.Trim().Split(EOD_SEPERATOR);

                    if (elements.Length < EOD_COLUMNS)
                    {
                        System.Diagnostics.Debug.WriteLine("COLUMNS<"+ EOD_COLUMNS + ": " + line);
                        continue;
                    }

                    symbol = elements[EOD_SYMBOL];
                    ratio = elements[EOD_RATIO].Split(EOD_RATIOSPLITER);

                    if (ratio.Length != 2)
                    {
                        System.Diagnostics.Debug.WriteLine("INVALID RATIO: " + line);
                        continue;
                    }

                    if (!Int32.TryParse(ratio[0], out recieves))
                    {
                        System.Diagnostics.Debug.WriteLine("INVALID recieves: " + ratio[0]);
                        continue;
                    }

                    if (!Int32.TryParse(ratio[1], out from))
                    {
                        System.Diagnostics.Debug.WriteLine("INVALID from: " + ratio[1]);
                        continue;
                    }

                    if (!DateTime.TryParseExact(elements[EOD_DATE], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date))
                    {
                        System.Diagnostics.Debug.WriteLine("INVALID date: " + elements[EOD_DATE]);
                        continue;
                    }


                    split = new Split(symbol, date, recieves, from);
                    
                    // save this split element
                    if (!this.ContainsKey(symbol))
                        this.Add(symbol, split);
                    else
                        Console.WriteLine("{0} exists OLD({1}) NEW({2})", symbol, this[symbol], split);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }


    }
}
